package com.xique.door.util;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.ObjectMetadata;
import com.xique.common.core.enums.StatusCode;
import com.xique.common.core.utils.AliossUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import ws.schild.jave.*;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @author shd
 * @create 2022/7/6 15:58
 */
@Component
@Slf4j
public class DoorAliossUtil extends AliossUtil {

    @Value("${alioss.endpoint}")
    private String endPoint;

    @Value("${alioss.accessKeyId}")
    private String accessKeyId;

    @Value("${alioss.accessKeySecret}")
    private String accessKeySecret;

    @Value("${alioss.park.bucketName}")
    private String bucketName;

    public Map<String, Object> uploadFileTranscoding(MultipartFile uploadFile, String prefix)
            throws Exception {
        Map<String, Object> returnMap = new HashMap<>();
        // 返回图片上传后返回的url
        String returnImgeUrl = "";

        String suffix = uploadFile.getOriginalFilename();
        File tempFile =
                File.createTempFile(com.xique.common.core.text.UUID.randomUUID().toString(), suffix);
        // 将前端上传的MultipartFile转为File
        uploadFile.transferTo(tempFile);
        // 如果是视频需要转码的文件
        File file =
                File.createTempFile(com.xique.common.core.text.UUID.randomUUID().toString(), suffix);
        // TODO 根据你的逻辑来判断文件是不是视频
        AudioAttributes audio = new AudioAttributes();
        // 音频编码格式
        audio.setCodec("libmp3lame");
        audio.setBitRate(800000);
        audio.setChannels(1);
        VideoAttributes video = new VideoAttributes();
        // 编码格式
        video.setCodec("libx264");
        video.setBitRate(3200000);
        // 帧率
        video.setFrameRate(15);
        EncodingAttributes attrs = new EncodingAttributes();
        attrs.setFormat("mp4");
        // 设置音频
        attrs.setAudioAttributes(audio);
        // 设置视频画面
        attrs.setVideoAttributes(video);
        Encoder encoder = new Encoder();

        MultimediaObject multimediaObject = new MultimediaObject(tempFile);
        encoder.encode(multimediaObject, file, attrs);
        // 上传到阿里云OSS
        // 获取文件原名称
        String originalFilename = uploadFile.getOriginalFilename();
        // 获取文件类型
        String fileType = originalFilename.substring(originalFilename.lastIndexOf("."));
        // 新文件名称
        String newFileName = UUID.randomUUID().toString() + fileType;
        // 构建日期路径, 例如：OSS目标文件夹/2020/10/31/文件名
        String filePath = new SimpleDateFormat("yyyy/MM/dd").format(new Date());
        // 文件上传的路径地址
        String uploadImgeUrl = prefix + "/" + filePath + "/" + newFileName;
        ObjectMetadata meta = new ObjectMetadata();
        meta.setContentType("video/mp4");
        // 文件上传至阿里云OSS
        OSSClient ossClient = new OSSClient(endPoint, accessKeyId, accessKeySecret);
        ossClient.putObject(bucketName, uploadImgeUrl, file, meta);

        // 获取文件上传后的图片返回地址
        returnImgeUrl = "http://" + bucketName + "." + endPoint + "/" + uploadImgeUrl;
        log.info("固定路径:" + returnImgeUrl);

        returnMap.put("code", StatusCode.SUCCESS.getCode());
        returnMap.put("url", returnImgeUrl);
        returnMap.put("objectName", uploadImgeUrl);
        tempFile.delete();
        file.delete();
        return returnMap;
    }

}
